// import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
import Notify from '@vant/weapp/notify/notify';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    radio: '1',
    showButton: true,
    // 勾选的答案id集合
    chooseOption: [],
    //所有答案集合
    allAnswer: [],
    // 默认展示第一题
    defaultQu: [],
    // 所有题目
    questionList: [],
    // 题目下标
    questionIndex: 0,

    // 已答的题目
    answered: [],
    // 待考试卷id
    waitPaperId: 0,
    //页面传参
    params: {},
    //关键字
    keyword: []
  },
  onChange(event) {
    console.log(event)
    // 记录所有答题信息
    this.setData({
      chooseOption: event.detail,
    });
  },
  onClick(event){
    console.log(event)
    const { name } = event.currentTarget.dataset;
    this.setData({
      chooseOption: name,
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  noop() { },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      waitPaperId: wx.getStorageSync('examPaperId')
    })
    //获取试卷信息
    wx.http.get(getApp().globalData.queryQuestion, {
      examPaperId: wx.getStorageSync('examPaperId')
    }).then(res => {
      if (res.code === 200) {
        console.log(res.data.paperQus)
        this.setData({
          questionList: res.data.paperQus,
          defaultQu: res.data.paperQus[0].qu,
          keyword: res.data.paperQus[0].qu.quAnswers[0].quAnswerContent.split(',')
        })
      }
    })
    wx.setNavigationBarTitle({
      title: '答题',
    });
    // 默认取第一题
    this.setData({
      defaultQu: this.data.questionList[this.data.questionIndex]
    });
  },
  // 交卷
  handPaper() {
    // 请求到后台打分
    let postData = {};
    postData.examPaperId = wx.getStorageSync('examPaperId');
    postData.previewPaperList = this.data.answered;
    wx.http.post(getApp().globalData.calculationScore, JSON.stringify(postData)).then(res => {
      if (res.code === 200) {
        wx.setStorageSync('examResultId', res.data.examResultId);
        wx.redirectTo({
          url: '/packageB/pages/syudy/examResult/index',
        })
      }
    })
  },
  // 下一题
  downQuestion(e) {
    if (this.data.chooseOption.length == 0) {
      Notify('请选择答案')
      return
    }
    if (index === this.data.questionList.length - 1) {
      Notify('已经是最后一题了')
      return
    }

    let index = this.data.questionIndex;
    let currentQu = this.data.questionList[index];
    //更新索引
    index++;
    this.setData({
      questionIndex: index
    });
    // 缓存已答题目信息
    this.saveAnswer(currentQu, index, this.data.chooseOption, 'down');
    //展示下一题的题目信息
    this.showNextQu(index);

  },
  // 上一题
  upQuestion() {
    let index = this.data.questionIndex;
    let currentQu = this.data.questionList[index];
    // 如果是最后一题
    if (index == 0) {
      Notify('已经是第一题了')
      return
    }
    index--;
    this.setData({
      questionIndex: index
    });
    // 缓存答案
    this.saveAnswer(currentQu, index, this.data.chooseOption, 'up');
    //展示上一题的题目信息
    this.showNextQu(index);
  },
  // 监听简答题答案输入
  onDescribe(e) {
    this.setData({ chooseOption: e.detail.value });
  },
  //展示下一题题目信息
  showNextQu(index) {
    let nextQuestion = this.data.questionList[index].qu;
    let answer = [];
    //获取缓存答案
    let answered = wx.getStorageSync('answered');
    if (answered != '') {
      const result = answered.find(item => item.quId == nextQuestion.quId)
      if (result != undefined) {
        answer.push(result.chooseOption)
      } else {
        // 清空复选框
        this.setData({
          chooseOption: []
        });
      }
    }
    console.log(answer)
    this.setData({
      defaultQu: nextQuestion,
      keyword: nextQuestion.quAnswers[0].quAnswerContent.split(','),
      chooseOption: answer
    });
  },
  //缓存已答的题目信息
  saveAnswer(currentQu, index, chooseOption, type) {
    let answer = wx.getStorageSync('answered');
    if (answer == '') {
      answer = [];
    }
    let question = {
      quId: currentQu.quId,
      chooseOption: chooseOption,
      quType: currentQu.qu.quType
    }
    //选项排序
    if (currentQu.quType == 2) {
      this.data.chooseOption.sort((a, b) => a - b);
      answer = this.data.chooseOption.join(',')
      //先判断题目是否已经缓存
      const result = answer.find(item => item.quId == currentQu.quId)
      if (result == undefined && chooseOption != '') {
        answer.push(question)
      }
      //简答题
    } else {
      //先判断题目是否已经缓存
      const result = answer.find(item => item.quId == currentQu.quId)
      if (result == undefined && chooseOption != '') {
        answer.push(question)
      }
    }
    wx.setStorageSync('answered', answer);
    this.setData({
      answered: answer
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})